//
//  PywRegisterSuccessViewController.m
//  PywSdk2.2_Test
//
//  Created by smallwave on 16/8/1.
//  Copyright © 2016年 yyft. All rights reserved.
//

#import "PywRegisterSuccessViewController.h"
#import "UIImage+Pyw.h"
#import "UILabel+Pyw.h"
#import "UIButton+Pyw.h"
#import "ApiClientConst.h"
#import "MutableDictionary.h"

#define EMOJI_W [UIImage imageWithName:@"img_emoji01"].size.width
#define EMOJI_H [UIImage imageWithName:@"img_emoji01"].size.height

@interface PywRegisterSuccessViewController ()

@property (nonatomic, strong) UIImageView   *emojiImg;                  // 表情图片
@property (nonatomic, strong) UILabel       *propmtLabel;               // 提示信息
@property (nonatomic, strong) UIButton      *loginButton;               // 立即登录按钮
@property (nonatomic, strong) UIButton      *changePhoneRegisterButton; // 切换手机号注册按钮

@property (nonatomic, copy) NSString        *phoneNumber;               // 手机号码

@end

@implementation PywRegisterSuccessViewController

#pragma mark - Init Method
- (instancetype)initWithPhoneNumber:(NSString *)phoneNumber{
    self = [super init];
    if (self) {
        self.phoneNumber = phoneNumber;
    }
    return self;
}

#pragma mark - Lift Cycle Method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}

#pragma mark - prepareUI
- (void)setUp{
    self.title = @"温馨提示";
    [self addSubviews];
}
- (void)addSubviews{
    [self.view addSubview:self.emojiImg];
    [self.view addSubview:self.propmtLabel];
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.changePhoneRegisterButton];
}

#pragma mark - Action
- (void)loginPassportAction:(UIButton *)action{
    [self.view endEditing:YES];
    NSString *passport = [self.phoneNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    // 立即登录（登录通信证）
    NSLog(@"%s",__FUNCTION__);
    [self dismissViewControllerAnimated:YES
                             completion:^{
                                 MutableDictionary *param = [[MutableDictionary alloc] init];
                                 [param setObject:passport forKey:@"passport"];
                                 [param setObject:@"" forKey:@"password"];
                                 
                                 [[NSNotificationCenter defaultCenter] postNotificationName:DidRegisterSuccess
                                                                                     object:param];
                             }];
}
- (void)changePhoneRegisterAction:(UIButton *)action{
    [self.view endEditing:YES];
    // 切换回“快速注册（1/3）”
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - layoutSubviews
/// 竖屏布局
- (void)verticalScreenControlsDistribution{
    self.propmtLabel.textAlignment = NSTextAlignmentCenter;
    CGFloat width = self.v_width;
    CGFloat navHeight = self.navHeight;
    
    CGFloat btnW = (width * 0.9);
    CGFloat btnH = 40;
    CGFloat labelH = 60;
    
    CGFloat emojiX = (width - EMOJI_W) * 0.5;
    CGFloat emojiY = navHeight + 30;
    self.emojiImg.frame = CGRectMake(emojiX, emojiY, EMOJI_W, EMOJI_H);
    
    CGFloat labelX = (width - btnW) * 0.5;
    CGFloat labelY = CGRectGetMaxY(self.emojiImg.frame);
    self.propmtLabel.frame = CGRectMake(labelX, labelY, btnW, labelH);
    
    CGFloat loginBtnX = labelX;
    CGFloat loginBtnY = CGRectGetMaxY(self.propmtLabel.frame) + 20;
    self.loginButton.frame = CGRectMake(loginBtnX, loginBtnY, btnW, btnH);
    
    CGFloat changeBtnX = loginBtnX;
    CGFloat changeBtnY = CGRectGetMaxY(self.loginButton.frame) + 20;
    self.changePhoneRegisterButton.frame = CGRectMake(changeBtnX, changeBtnY, btnW, btnH);
    
}
/// 横屏布局
- (void)horizontalScreenControlsDistribution{
    self.propmtLabel.textAlignment = NSTextAlignmentLeft;
    CGFloat width = self.h_width;
    CGFloat height = self.h_height;
    
    CGFloat btnW = (width * 0.4);
    CGFloat btnH = 40;
    
    CGFloat bindBtnX = (width * 0.5) + 10;
    CGFloat bindBtnY = height * 0.5;
    self.loginButton.frame = CGRectMake(bindBtnX, bindBtnY, btnW, btnH);
    
    CGFloat changeBtnX = (width * 0.5) - btnW - 10;
    CGFloat changeBtnY = bindBtnY;
    self.changePhoneRegisterButton.frame = CGRectMake(changeBtnX, changeBtnY, btnW, btnH);
    
    CGFloat emojiX = changeBtnX + ((btnW - EMOJI_W) * 0.5);
    CGFloat emojiY = changeBtnY - 20 - EMOJI_H;
    self.emojiImg.frame = CGRectMake(emojiX, emojiY, EMOJI_W, EMOJI_H);
    
    CGFloat labelX = CGRectGetMaxX(self.emojiImg.frame) + 10;
    CGFloat labelY = emojiY + 10;
    CGFloat labelW = width * 0.6;
    self.propmtLabel.frame = CGRectMake(labelX, labelY, labelW, EMOJI_H - 10);
}

#pragma mark - lazyLoad
- (UIImageView *)emojiImg{
    if (!_emojiImg) {
        _emojiImg = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"img_emoji01"]];
    }
    return _emojiImg;
}
- (UILabel *)propmtLabel{
    if (!_propmtLabel) {
        _propmtLabel = [UILabel labelWithText:nil
                                textAlignment:NSTextAlignmentCenter
                                     fontSize:16
                                    textColor:[UIColor blackColor]];
        _propmtLabel.text = @"友友的手机号码已被注册!";
    }
    return _propmtLabel;
}
- (UIButton *)loginButton{
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithBackgroundColor:RGB(252, 177, 14, 1)
                                              cornerRadius:20
                                                titleColor:[UIColor whiteColor]
                                        titleLabelFontSize:18
                                                     title:@"立即登录"];
        
        [_loginButton addTarget:self action:@selector(loginPassportAction:) forControlEvents:UIControlEventTouchUpInside];
        [_loginButton setAccessibilityIdentifier:@"phonereg_login"];
    }
    return _loginButton;
}
- (UIButton *)changePhoneRegisterButton{
    if (!_changePhoneRegisterButton) {
        _changePhoneRegisterButton = [UIButton buttonWithBackgroundColor:[UIColor whiteColor]
                                                            cornerRadius:20
                                                              titleColor:[UIColor blackColor]
                                                      titleLabelFontSize:18
                                                                   title:@"换其他手机号注册"];
        _changePhoneRegisterButton.layer.borderWidth = 0.5;
        _changePhoneRegisterButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [_changePhoneRegisterButton addTarget:self action:@selector(changePhoneRegisterAction:) forControlEvents:UIControlEventTouchUpInside];
        [_changePhoneRegisterButton setAccessibilityIdentifier:@"phonereg_changephone"];
    }
    return _changePhoneRegisterButton;
}

@end
